home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gnome_sudoku / timer.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  7.6 KB  |  179 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import gtk
  5. import gobject
  6. import gtk.glade as gtk
  7. import os
  8. import os.path as os
  9. import time
  10. from gettext import gettext as _
  11. from gettext import ngettext
  12. from defaults import *
  13.  
  14. def format_time(time, round_at = None, friendly = False):
  15.     """Format a time for display to the user.
  16.  
  17.     If round_at, we round all times to some number of seconds.
  18.  
  19.     If friendly, we don't bother showing the user more than two
  20.     units. i.e. 3 days 2 hours, or 2 minutes 30 seconds, but not 3
  21.     days, 4 hours, 2 minutes and 3 seconds...
  22.     """
  23.     time = int(time)
  24.     time_strings = []
  25.     units = [
  26.         (int(3.15576e+07), (lambda years: ngettext('%(n)s year', '%(n)s years', years) % {
  27. 'n': years })),
  28.         (2678400, (lambda months: ngettext('%(n)s month', '%(n)s months', months) % {
  29. 'n': months })),
  30.         (604800, (lambda weeks: ngettext('%(n)s week', '%(n)s weeks', weeks) % {
  31. 'n': weeks })),
  32.         (86400, (lambda days: ngettext('%(n)s day', '%(n)s days', days) % {
  33. 'n': days })),
  34.         (3600, (lambda hours: ngettext('%(n)s hour', '%(n)s hours', hours) % {
  35. 'n': hours })),
  36.         (60, (lambda minutes: ngettext('%(n)s minute', '%(n)s minutes', minutes) % {
  37. 'n': minutes })),
  38.         (1, (lambda seconds: ngettext('%(n)s second', '%(n)s seconds', seconds) % {
  39. 'n': seconds }))]
  40.     for divisor, unit_formatter in units:
  41.         time_covered = time / divisor
  42.         if time_covered:
  43.             if round_at and len(time_strings) + 1 >= round_at:
  44.                 time_covered = int(round(float(time) / divisor))
  45.                 time_strings.append(unit_formatter(time_covered))
  46.                 break
  47.             else:
  48.                 time_strings.append(unit_formatter(time_covered))
  49.                 time = time - time_covered * divisor
  50.         len(time_strings) + 1 >= round_at
  51.     
  52.     if friendly and len(time_strings) > 2:
  53.         time_stings = time_strings[:2]
  54.     
  55.     if len(time_strings) > 2:
  56.         return _(' and ').join([
  57.             _(', ').join(time_strings[0:-1]),
  58.             time_strings[-1]])
  59.     return _(' ').join(time_strings)
  60.  
  61.  
  62. def format_time_compact(tim):
  63.     tim = int(tim)
  64.     hours = tim / 3600
  65.     minutes = (tim % 3600) / 60
  66.     seconds = tim % 3600 % 60
  67.     return '%d:%02d:%02d' % (hours, minutes, seconds)
  68.  
  69.  
  70. def format_date(tim):
  71.     lt = time.localtime(tim)
  72.     hours = int(time.strftime('%H', lt))
  73.     minutes = int(time.strftime('%M', lt))
  74.     diff = time.time() - tim
  75.     to_yesterday = hours * 60 * 60 + minutes * 60
  76.     if diff < to_yesterday:
  77.         return time.strftime(_('Today %R %p'), lt)
  78.     if diff < to_yesterday + 86400:
  79.         return time.strftime(_('Yesterday %R %p'), lt)
  80.     if diff < 604800:
  81.         return time.strftime(_('%A %H:%M'), lt)
  82.     return time.strftime(_('%A %B %d %R %p'), lt)
  83.  
  84.  
  85. def format_friendly_date(tim):
  86.     lt = time.localtime(tim)
  87.     hours = int(time.strftime('%H', lt))
  88.     minutes = int(time.strftime('%M', lt))
  89.     diff = time.time() - tim
  90.     ct = time.localtime()
  91.     chour = ct[3]
  92.     cmin = ct[4]
  93.     to_yesterday = chour * 60 * 60 + cmin * 60
  94.     if diff < to_yesterday:
  95.         if diff < 3600:
  96.             if int(diff) / 60:
  97.                 m = diff / 60
  98.                 return ngettext('%(n)s minute ago', '%(n)s minutes ago', m) % {
  99.                     'n': int(m) }
  100.             return ngettext('%(n)s second ago', '%(n)s seconds ago', int(diff)) % {
  101.                 'n': int(diff) }
  102.         diff < 3600
  103.         return time.strftime(_('at %I:%M %p'), lt)
  104.     diff < to_yesterday
  105.     if diff < to_yesterday + 86400:
  106.         return time.strftime(_('yesterday at %I:%M %p'), lt)
  107.     if diff < to_yesterday + 518400:
  108.         return time.strftime(_('%A %I:%M %p'), lt)
  109.     return time.strftime(_('%B%e'), lt)
  110.  
  111.  
  112. class ActiveTimer(gobject.GObject):
  113.     '''A timer to keep track of how much time a window is active.'''
  114.     __gsignals__ = {
  115.         'timing-started': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
  116.         'timing-stopped': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()) }
  117.     
  118.     def __init__(self, window):
  119.         gobject.GObject.__init__(self)
  120.         self.window = window
  121.         self.timing_running = False
  122.         self.__absolute_start_time__ = 0
  123.         self.tot_time = 0
  124.         self.tot_time_complete = 0
  125.         self.window.connect('window-state-event', self.window_state_event_cb)
  126.         self.window.connect('state-changed', self.window_state_event_cb)
  127.         self.window.connect('visibility-notify-event', self.window_state_event_cb)
  128.         self.window.connect('expose-event', self.window_state_event_cb)
  129.         self.window.connect('no-expose-event', self.window_state_event_cb)
  130.  
  131.     
  132.     def window_state_event_cb(self, *args):
  133.         if self.window.is_active():
  134.             self.toggle_timing(True)
  135.         else:
  136.             self.toggle_timing(False)
  137.  
  138.     
  139.     def toggle_timing(self, on):
  140.         if not self.__absolute_start_time__:
  141.             return None
  142.         if on and not (self.timing_running):
  143.             self.timing_started_at = time.time()
  144.             self.timing_running = True
  145.             self.emit('timing-started')
  146.         
  147.  
  148.     
  149.     def start_timing(self):
  150.         self.timing_running = False
  151.         self.__absolute_start_time__ = 0
  152.         self.tot_time = 0
  153.         self.tot_time_complete = 0
  154.         self.__absolute_start_time__ = time.time()
  155.         self.toggle_timing(True)
  156.  
  157.     
  158.     def finish_timing(self):
  159.         self.toggle_timing(False)
  160.         if self.tot_time < 1:
  161.             self.tot_time = 1
  162.         
  163.         if self.tot_time > self.tot_time_complete:
  164.             self.tot_time_complete = self.tot_time
  165.         
  166.  
  167.     
  168.     def active_time_string(self):
  169.         return format_time(self.tot_time)
  170.  
  171.     
  172.     def total_time_string(self):
  173.         return format_time(self.tot_time_complete)
  174.  
  175.  
  176. if gtk.pygtk_version[1] < 8:
  177.     gobject.type_register(ActiveTimer)
  178.  
  179.